package com.cjx.minio.util;

import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
*@Description 文件工具类
*@Verson v1.0.0
*@Author cjunxian
*@Date 2020/10/29
*/
public class FileUtils {

    /**
     * 获取文件后缀，带（.）
     *
     * @param fileName 文件名
     * @return 后缀 extend
     */
    public static String getExtend(String fileName) {
        if (StringUtils.isEmpty(fileName)) {
            return "";
        }
        int iPos = fileName.lastIndexOf('.');
        if (iPos < 0) {
            return "";
        }
        return fileName.substring(iPos).toLowerCase();
    }

    public static File createTempFile(InputStream inputStream, String fileName){

        //防止多线程情况下重名
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String time = sdf.format(date);
        //文件名上加上日期，防止多线程时文件被覆盖
        try {
            //创建临时文件temp
            //将inputStream写进temp;
            File temp = File.createTempFile("video"+time,getExtend(fileName));
            OutputStream outputStream = new FileOutputStream(temp);
            int index;
            byte[] buffer = new byte[1024];
            while ((index = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, index);
            }
            outputStream.flush();
            inputStream.close();
            outputStream.close();
            return temp;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static File  saveToLocalFile(MultipartFile file) {
        try {
            //防止多线程情况下重名
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            String time = sdf.format(date);
            //文件名上加上日期，防止多线程时文件被覆盖
            InputStream inStream = file.getInputStream();
            File tempFile = File.createTempFile("video"+time , getExtend(file.getOriginalFilename()));
            FileOutputStream fw = new FileOutputStream(tempFile);
            byte[] bytes = new byte[1024];
            int len;
            while ((len = inStream.read(bytes, 0, bytes.length)) != -1) {
                fw.write(bytes, 0, len);
            }
            fw.flush();
            inStream.close();
            fw.close();
            return tempFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断是否属于可预览格式
     * @param fileName
     * @return
     */
    public static boolean canPreView(String fileName) {

        if (fileName == null || fileName.length() == 0) {
            return false;
        }

        int extIndex = fileName.lastIndexOf(".");
        if (extIndex == -1) {
            return false;
        }

        final String ext = fileName.substring(extIndex);

        return Arrays.stream(new String[] {".flv", ".avi", ".mp4",".jpg",".png"})
                .anyMatch(p -> p.equalsIgnoreCase(ext));
    }
}
